package com.szjlc.mybatis.sqlinterceptor.result;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.http.HttpStatus;

/**
 * 全局统一返回结果类
 * @author steve
 * @since 2022-02-20 12:13:25
 */

@SuppressWarnings("unused")
@Data
@ApiModel(description = "接口统一响应类型")
public class BusinessResult<T> {

    /** 状态响应码 */
    @ApiModelProperty(value = "状态响应码")
    private Integer code;

    /** 响应信息 */
    @ApiModelProperty(value = "响应信息")
    private String message;

    /** 响应数据 */
    @ApiModelProperty(value = "响应数据")
    private T data;

    /** 时间戳 */
    @ApiModelProperty(value = "时间戳")
    private Long timeStamp;

    public BusinessResult() {
    }

    public BusinessResult(Integer code, String message, T data, Long timeStamp) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.timeStamp = timeStamp;
    }

    /** 请求成功 */
    public static <T> BusinessResult<T> success(T data) {
        return new BusinessResult(HttpStatus.OK.value(), "操作成功", data, System.currentTimeMillis());
    }


    /** 请求失败 */
    public static <T> BusinessResult<T> fail(Integer code, String message) {
        return new BusinessResult(code, message, null, System.currentTimeMillis());
    }

    /** 请求失败 */
    public static <T> BusinessResult<T> fail(HttpStatus httpStatusEnum) {
        return fail(httpStatusEnum.value(), httpStatusEnum.getReasonPhrase());
    }
}

